/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.howtimeflies.simplecrypt;

import java.io.Closeable;
import java.io.File;
import java.io.FileFilter;
import java.util.List;

/**
 *
 *
 * Created by zhangg on 2015/5/25 0025 16:21.
 *
 * @author <a href=mailto:zhanggeng@howtimeflies.org>GengZhang</a>
 */
public abstract class Cryptor {

    protected final Algorithm algorithm;

    protected Cryptor(Algorithm algorithm) {
        this.algorithm = algorithm;
    }

    protected void closeQuite(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    protected void listFile(File parent, boolean recursive, FileFilter fileFilter, List<File> allFile) {
        File[] files = parent.listFiles();
        for (File file : files) {
            if (file.isDirectory() && recursive) {
                listFileRecursive(file, recursive, fileFilter, allFile);
            } else {
                allFile.add(file);
            }
        }
    }

    private void listFileRecursive(File parent, boolean recursive, FileFilter fileFilter, List<File> allFile) {
        File[] files = parent.listFiles(fileFilter);
        for (File file : files) {
            if (file.isDirectory() && recursive) {
                listFile(file, recursive, fileFilter, allFile);
            } else {
                allFile.add(file);
            }
        }
    }

    protected File checkFolder(String folderPath, boolean checkExist, boolean makeWhenNotExist,
                             boolean checkRead, boolean checkWrite) {
        File file = new File(folderPath);
        if (checkExist && !file.exists()) {
            if (makeWhenNotExist) {
                file.mkdirs();
            } else {
                throw new IllegalArgumentException(folderPath + " is not exist");
            }
        }
        if (!file.isDirectory()) {
            throw new IllegalArgumentException(folderPath + " is not a directory");
        }
        if (checkRead && !file.canRead()) {
            throw new IllegalArgumentException(folderPath + " can not read");
        }
        if (checkWrite && !file.canWrite()) {
            throw new IllegalArgumentException(folderPath + " can not write");
        }
        return file;
    }

}
